/*
 * @See https://github.com/sharegate/craco
 * @Document https://github.com/sharegate/craco/blob/master/packages/craco/README.md#configuration-overview
 */

const path = require('path');
const mockRouter = require('./mock');
const nyanProgressPlugin = require('nyan-progress-webpack-plugin');
const webpackBundleAnalyzer = require('webpack-bundle-analyzer');
const { paths, ESLINT_MODES, POSTCSS_MODES } = require('@craco/craco');
const { BundleAnalyzerPlugin } = webpackBundleAnalyzer;

const setProgress = number => {
    const str = String(number);
    const result = str.substring(0, 4);
    return `${Number(result) * 100}%`;
};

module.exports = {
    webpack: {
        // 设置 alias别名
        alias: {
            '@': path.resolve(__dirname, `${paths.appSrc}/`)
        },
        plugins: [
            // new BundleAnalyzerPlugin(),
            // webpack 编译进度条
            new nyanProgressPlugin({
                // https://sebastianblade.com/say-farewell-to-the-boring-webpack-console-interface/
                debounceInterval: 60,
                nyanCatSays(progress, messages) {
                    return `哟 又写Bug ${setProgress(progress)} `;
                }
            })
        ],
        configure: (webpackConfig, { env, paths }) => {
            // vendor
            // webpackConfig.entry = {
            //     app: `${paths.appPath}/src/index.js`,
            //     vendor:['axios']
            // };
            // webpackConfig.optimization.minimize = true;
            // webpackConfig.optimization.splitChunks.name = "";
            // webpackConfig.optimization.splitChunks.test = /[\\/]node_modules[\\/]/;
            // webpackConfig.optimization.splitChunks.chunks = "all";

            // console.log(paths)
            // console.log(webpackConfig)

            // 设置build输出目录
            const newPath = `${paths.appPath}/dist`;
            webpackConfig.output.path = newPath;
            paths.appBuild = newPath;
            if (process.env.NODE_ENV === 'production') {
                webpackConfig.devtool = false;
            }
            return webpackConfig;
        }
    },
    plugins: [
        {
            // antd组件库插件支持
            plugin: require('craco-antd')
        }
    ],
    babel: {
        loaderOptions: babelLoaderOptions => {
            // 加载根目录下的 .babelrc
            babelLoaderOptions.babelrc = true;
            return babelLoaderOptions;
        }
    },
    eslint: {
        // 加载根目录下的 .eslintrc.js
        mode: ESLINT_MODES.file,
        loaderOptions: eslintOptions => {
            // 加载根目录下的 .eslintignore
            eslintOptions.ignore = true;
            return eslintOptions;
        }
    },
    style: {
        postcss: {
            // 加载根目录下的 postcss.config.js
            mode: POSTCSS_MODES.file
        },
        less: {
            loaderOptions: {
                modifyVars: {},
                javascriptEnabled: true
            }
        }
    },
    devServer: {
        port: 9000,
        before(app) {
            mockRouter(app);
        }
    }
};
